package com.z6school.manage.service.impl;

import com.z6school.manage.common.annotation.RequiredLog;
import com.z6school.manage.common.exception.ServiceException;
import com.z6school.manage.common.pojo.DeptMajorClass;
import com.z6school.manage.common.pojo.Nodes;
import com.z6school.manage.mapper.ClassMapper;
import com.z6school.manage.mapper.DeptMapper;
import com.z6school.manage.mapper.MajorMapper;
import com.z6school.manage.po.Clas;
import com.z6school.manage.po.Dept;
import com.z6school.manage.po.Majors;
import com.z6school.manage.service.DMSService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DMSServiceImpl implements DMSService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private MajorMapper majorMapper;
    @Autowired
    private ClassMapper classMapper;
    @Override
    @RequiredLog(operation = "查询校建信息")
    @RequiresPermissions("sys:dept:view")
    public List<DeptMajorClass> findAll() {
        List<DeptMajorClass> list1 = deptMapper.findAll();
        List<DeptMajorClass> list2 = majorMapper.findAll();
        List<DeptMajorClass> list3 = classMapper.findAll();
        List<DeptMajorClass> list=new ArrayList<>();

        list.addAll(list1);
        list.addAll(list2);
        list.addAll(list3);

        return list;
    }

    @Override
    @RequiredLog(operation = "删除班级")
    public void deleteByClassId(Integer id) {
            classMapper.deleteByClassId(id);
    }

    @Override
    @RequiredLog(operation = "删除专业")
    public void deleteByMajorId(Integer id) {
        try {
            majorMapper.deleteByMajorId(id);
        }catch (Exception e){
            throw new ServiceException("请先删除所辖班级");
        }
    }

    @Override
    @RequiredLog(operation = "删除系部")
    public void deleteByDeptId(Integer id) {
        try {
            deptMapper.deleteByDeptId(id);
        }catch (Exception e){
            throw new ServiceException("请先删除所辖专业");
        }
    }

    @Override
    @RequiredLog(operation = "查询树节点")
    public List<Nodes> findZTreeNodes() {
        List<Nodes> list1 = deptMapper.findZTreeNodes();
        List<Nodes> list2 = majorMapper.findZTreeNodes();
       // List<Nodes> list3 = classMapper.findZTreeNodes();

        List<Nodes> list=new ArrayList<>();
        list.addAll(list1);
        list.addAll(list2);
        //list.addAll(list3);
        return list;
    }

    @Override
    @RequiredLog(operation = "新增校建信息")
    public void saveObject(DeptMajorClass deptMajorClass) {
        if (deptMajorClass.getParentId()==null){
            Dept dept =new Dept();
            dept.setName(deptMajorClass.getName())
                    .setNote(deptMajorClass.getNote())
                    .setSort(deptMajorClass.getSort())
                    .setParentId(deptMajorClass.getParentId())
                    .setParentName(deptMajorClass.getParentName());
            deptMapper.insert(dept);
        }
        if (deptMajorClass.getParentId()<100){
            Majors major=new Majors();
            major.setName(deptMajorClass.getName())
                    .setNote(deptMajorClass.getNote())
                    .setSort(deptMajorClass.getSort())
                    .setParentId(deptMajorClass.getParentId())
                    .setParentName(deptMajorClass.getParentName());
            majorMapper.insert(major);
        }
    }

    @Override
    @RequiredLog(operation = "更新校建信息")
    public void updateObject(DeptMajorClass deptMajorClass) {
        if (deptMajorClass.getId()<100) {
            Dept dept =new Dept();
            dept.setId(deptMajorClass.getId())
                    .setName(deptMajorClass.getName())
                    .setNote(deptMajorClass.getNote())
                    .setSort(deptMajorClass.getSort())
                    .setParentId(deptMajorClass.getParentId())
                    .setParentName(deptMajorClass.getParentName());
            deptMapper.updateById(dept);
        }
        if (deptMajorClass.getId()>100&&deptMajorClass.getId()<1000){
            Majors major=new Majors();
            major.setId(deptMajorClass.getId())
                    .setName(deptMajorClass.getName())
                    .setNote(deptMajorClass.getNote())
                    .setSort(deptMajorClass.getSort())
                    .setParentId(deptMajorClass.getParentId())
                    .setParentName(deptMajorClass.getParentName());
            majorMapper.updateById(major);
        }
        if (deptMajorClass.getId()>1000){
            Clas clas =new Clas();
            clas.setId(deptMajorClass.getId())
                    .setName(deptMajorClass.getName())
                    .setNote(deptMajorClass.getNote())
                    .setSort(deptMajorClass.getSort())
                    .setParentId(deptMajorClass.getParentId())
                    .setParentName(deptMajorClass.getParentName());
            classMapper.updateById(clas);

        }
    }


}
